/**
 * 初始化邀请码详情对话框
 */
var InvitationCodeInfoDlg = {
    invitationCodeInfoData : {}
};

/**
 * 清除数据
 */
InvitationCodeInfoDlg.clearData = function() {
    this.invitationCodeInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
InvitationCodeInfoDlg.set = function(key, val) {
    this.invitationCodeInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
InvitationCodeInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
InvitationCodeInfoDlg.close = function() {
    parent.layer.close(window.parent.InvitationCode.layerIndex);
}

/**
 * 收集数据
 */
InvitationCodeInfoDlg.collectData = function() {
    this
    .set('deptid')
    .set('number');
}

/**
 * 提交添加
 */
InvitationCodeInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/invitationCode/distribution", function(data){
        Feng.success("添加成功!");
        window.parent.InvitationCode.table.refresh();
        InvitationCodeInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.invitationCodeInfoData);
    ajax.start();
}
/**
 * 显示部门选择的树
 *
 * @returns
 */
InvitationCodeInfoDlg.showDepartmentSelectTree = function () {
    var deptObj = $("#dept");
    var deptPostion = $("#dept").offset();
    $("#menuContent").css({
        left: deptPostion.left + "px",
        top: deptPostion.top + deptObj.outerHeight() + "px"
    }).slideDown("fast");
    $("body").bind("mousedown", onBodyDown);
};

/**
 * 显示用户详情部门选择的树
 *
 * @returns
 */
InvitationCodeInfoDlg.showInfoCategorySelectTree = function () {
    var deptObj = $("#dept");
    var deptPostion = $("#dept").offset();
    $("#menuContent").css({
        left: deptPostion.left + "px",
        top: deptPostion.top + deptObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};

/**
 * 隐藏部门选择的树
 */
InvitationCodeInfoDlg.hideCategorySelectTree = function () {
    $("#menuContent").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(
            event.target).parents("#menuContent").length > 0)) {
        InvitationCodeInfoDlg.hideDeptSelectTree();
    }
}
InvitationCodeInfoDlg.hideDeptSelectTree = function () {
    $("#menuContent").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};
InvitationCodeInfoDlg.onClickDept = function (e, treeId, treeNode) {
    $("#dept").attr("value", instance.getSelectedVal());
    $("#deptid").attr("value", treeNode.id);
};
InvitationCodeInfoDlg.doExport =function (deptId) {
    window.location.href = Feng.ctxPath + "/invitationCode/export?deptId=" + deptId;
}
$(function() {
    var ztree = new $ZTree("treeDemo", "/department/tree");
    ztree.bindOnClick(InvitationCodeInfoDlg.onClickDept);
    ztree.init();
    instance = ztree;

});
